package com.zlzxm.shoesmarket.view.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.util.ZViewHelp;
import com.zlzxm.shoesmarket.view.activity.manage.ActivityIndex;
import com.zlzxm.shoesmarket.view.adapter.ADNote;
import com.zlzxm.shoesmarket.view.widget.simpleead.SimpleHead;
import com.zlzxm.shoesmarket.view.window.wheelpick.EntityOption;
import com.zlzxm.shoesmarket.view.window.wheelpick.OnOptionSelectListener;
import com.zlzxm.shoesmarket.view.window.wheelpick.WindowWheelPick;
import com.zlzxm.shoesmarket.viewmodel.VMNote;

import java.util.ArrayList;
import java.util.List;

/**
 * CREATE BY zlzxm ON 4/21/21
 * <br>
 *记账本
 */
public class ACNote extends BaseActivity implements View.OnClickListener {

    public static void start(Context context) {
        Intent starter = new Intent(context, ACNote.class);
        context.startActivity(starter);
    }

    private VMNote vmNote;

    private RecyclerView recyclerView;

    private ADNote adNote = new ADNote();

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.ac_note);
        SimpleHead simpleHead = ZViewHelp.findById(this,R.id.head);
        simpleHead.setOnItemclicklistener(this::finish);
        ZViewHelp.setStatusBarTransparent(getWindow(),false);

        recyclerView = ZViewHelp.findById(this,R.id.recyclerView);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));


        ZViewHelp.setOnClickListener(this,R.id.tvMonth,this);
        ZViewHelp.setOnClickListener(this,R.id.llAddNote,this);
    }

    @Override
    protected void initViewModule() {
        super.initViewModule();
        vmNote = new ViewModelProvider(this).get(VMNote.class);
        vmNote.liveNote.observe(this,adNote::setList);
        vmNote.liveMonth.observe(this,this::showWindow);
    }

    @Override
    protected void initData() {
        super.initData();
        recyclerView.setAdapter(adNote);
        vmNote.requestData();
    }


    public void showWindow(List<String> month){

        List<EntityOption> options = new ArrayList<>();

        for (String s : month) {

            options.add( new EntityOption("1",s));
        }

        WindowWheelPick.getInstance()
                .setOptions(options)
                .setOnOptionSelectListener(new OnOptionSelectListener() {
                    @Override
                    public void onSelected(EntityOption entityOption) {
                        vmNote.liveSelectedMonth.setValue(entityOption.getName());
                    }
                })
                .show(getSupportFragmentManager(),"");

    }

    @Override
    public void onClick(View v) {

        if(v.getId() == R.id.tvMonth){

            monthClicked();

        }else if(v.getId() == R.id.llAddNote){

            ActivityIndex.startACAddNote(this);

        }

    }


    private void monthClicked(){

        vmNote.requestMonth();

    }
}
